package com.gzc.just.play.last.war.robotclient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Robot Player class
 * Based on C++ Player class implementation
 */
public class RobotPlayer {
    private static final Logger logger = LoggerFactory.getLogger(RobotPlayer.class);
    
    // Player states
    public enum PlayerState {
        IDLE,       // Idle state
        MOVING,    // Moving state
        ATTACKING,  // Attacking state
        DEAD        // Dead state
    }
    
    // Basic properties
    private final long robotId;
    private final RobotClient client;
    private int heroId;
    private String heroName;
    private int level = 1;
    private int maxHp = 100;
    private int hp = 100;
    private int maxMp = 50;
    private int mp = 50;
    private int attack = 10;
    private int defense = 5;
    private double posX = 0.0;
    private double posY = 0.0;
    private double posZ = 0.0;
    private PlayerState currentState = PlayerState.IDLE;
    
    // Statistics
    private final AtomicLong lastStateChangeTime = new AtomicLong(0);
    private final AtomicLong deathCount = new AtomicLong(0);
    private final AtomicLong killCount = new AtomicLong(0);
    private final AtomicLong assistCount = new AtomicLong(0);
    
    // Inventory and equipment
    private final List<Integer> purchasedGoods = new ArrayList<>();
    private final Map<Integer, SkillInfo> skills = new HashMap<>();
    private long lockedTargetId = -1;
    
    public RobotPlayer(RobotClient client) {
        this.robotId = client.getRobotId();
        this.client = client;
        
        logger.info("Robot player created: id={}", robotId);
    }
    
    /**
     * Update state
     */
    public void updateState(PlayerState newState, double x, double y, double z) {
        if (currentState != newState) {
            currentState = newState;
            lastStateChangeTime.set(System.currentTimeMillis());
        }
        this.posX = x;
        this.posY = y;
        this.posZ = z;
    }
    
    /**
     * Update HP
     */
    public void updateHP(int hp) {
        this.hp = Math.max(0, Math.min(hp, maxHp));
        if (this.hp <= 0) {
            updateState(PlayerState.DEAD, posX, posY, posZ);
            deathCount.incrementAndGet();
        }
    }
    
    /**
     * Update MP
     */
    public void updateMP(int mp) {
        this.mp = Math.max(0, Math.min(mp, maxMp));
    }
    
    /**
     * Learn skill
     */
    public void learnSkill(int skillId, String skillName, int level, int manaCost) {
        SkillInfo skill = new SkillInfo(skillId, skillName, level, manaCost);
        skills.put(skillId, skill);
    }
    
    /**
     * Purchase goods
     */
    public void purchaseGoods(int goodsId) {
        purchasedGoods.add(goodsId);
    }
    
    /**
     * Get available skills
     */
    public List<SkillInfo> getAvailableSkills() {
        List<SkillInfo> available = new ArrayList<>();
        for (SkillInfo skill : skills.values()) {
            if (mp >= skill.manaCost) {
                available.add(skill);
            }
        }
        return available;
    }
    
    /**
     * Find nearest enemy
     */
    public long findNearestEnemy() {
        // TODO: Implement enemy search logic
        return -1;
    }
    
    /**
     * Lock target
     */
    public void lockTarget(long targetId) {
        this.lockedTargetId = targetId;
    }
    
    /**
     * Clear locked target
     */
    public void clearLockedTarget() {
        this.lockedTargetId = -1;
    }
    
    /**
     * Add kill
     */
    public void addKill() {
        killCount.incrementAndGet();
    }
    
    /**
     * Add assist
     */
    public void addAssist() {
        assistCount.incrementAndGet();
    }
    
    // Getters and Setters
    public long getRobotId() {
        return robotId;
    }
    
    public RobotClient getClient() {
        return client;
    }
    
    public int getHeroId() {
        return heroId;
    }
    
    public void setHeroId(int heroId) {
        this.heroId = heroId;
    }
    
    public String getHeroName() {
        return heroName;
    }
    
    public void setHeroName(String heroName) {
        this.heroName = heroName;
    }
    
    public int getLevel() {
        return level;
    }
    
    public void setLevel(int level) {
        this.level = level;
    }
    
    public int getHp() {
        return hp;
    }
    
    public int getMaxHp() {
        return maxHp;
    }
    
    public void setMaxHp(int maxHp) {
        this.maxHp = maxHp;
    }
    
    public int getMp() {
        return mp;
    }
    
    public int getMaxMp() {
        return maxMp;
    }
    
    public void setMaxMp(int maxMp) {
        this.maxMp = maxMp;
    }
    
    public int getAttack() {
        return attack;
    }
    
    public void setAttack(int attack) {
        this.attack = attack;
    }
    
    public int getDefense() {
        return defense;
    }
    
    public void setDefense(int defense) {
        this.defense = defense;
    }
    
    public double getPosX() {
        return posX;
    }
    
    public double getPosY() {
        return posY;
    }
    
    public double getPosZ() {
        return posZ;
    }
    
    public PlayerState getCurrentState() {
        return currentState;
    }
    
    public long getLastStateChangeTime() {
        return lastStateChangeTime.get();
    }
    
    public long getDeathCount() {
        return deathCount.get();
    }
    
    public long getKillCount() {
        return killCount.get();
    }
    
    public long getAssistCount() {
        return assistCount.get();
    }
    
    public List<Integer> getPurchasedGoods() {
        return purchasedGoods;
    }
    
    public Map<Integer, SkillInfo> getSkills() {
        return skills;
    }
    
    public long getLockedTargetId() {
        return lockedTargetId;
    }
    
    /**
     * Skill information
     */
    public static class SkillInfo {
        public final int skillId;
        public final String skillName;
        public final int level;
        public final int manaCost;
        
        public SkillInfo(int skillId, String skillName, int level, int manaCost) {
            this.skillId = skillId;
            this.skillName = skillName;
            this.level = level;
            this.manaCost = manaCost;
        }
    }
}